import { websocket_address } from '../configs/index'

function useWebSocket (handleMessage){
    const ws = new WebSocket(websocket_address);

    const init = () => {
        bindEvent();
    }

    function bindEvent () {
        ws.addEventListener('open', handleOpen, false);
        ws.addEventListener('close', handleClose, false);
        ws.addEventListener('error', handleError, false);
        ws.addEventListener('message', handleMessage, false);
    }

    function handleOpen(e) {
        console.log('Websocket open',e);
    }
    function handleClose(e) {
        console.log('Websocket close',e);
    }
    function handleError(e) {
        console.log('Websocket error',e);
    }

    init();

    return ws;
}

export { useWebSocket } ;